import { isvalidatemobile, cardid } from "@/util/validate";

const REGEXP_FLOAT = /^(-)?([0-9]|\.)+$/;
const REGEXP_CH = /^[\u4e00-\u9fa5]+$/;
const REGEXP_EMPTY = /^(<(\/?)p>|<(\/?)br>|&nbsp;)+$/;
const REGEXP_PRICE = /((^[1-9]\d*)|^0)(\.\d{0,2}){0,1}$/;
export const checkEmptyEdit = (_, value, callback) => {
  if (!value) {
    return callback(new Error("请编辑内容"));
  }
  if (REGEXP_EMPTY.test(value.replace(/\s/g, ""))) {
    return callback(new Error("请编辑内容"));
  }
  callback();
};

export const checkNumber = (rule, value, callback) => {
  if (rule.required && !value && value !== 0) {
    return callback(new Error("不能为空"));
  }
  if (typeof value !== "number") {
    return callback(new Error("请输入数字"));
  }
  callback();
};

export const checkHundred = (rule, value, callback) => {
  if (rule.required && !value && value !== 0) {
    callback("不能为空");
    return;
  }
  if (typeof value !== "number" || value > 100) {
    return callback(new Error("请输入正确的数值"));
  }
  callback();
};

export const checkPhone = (rule, value, callback) => {
  if (rule.required && !value) {
    callback("手机号不能为空");
    return;
  }
  const result = isvalidatemobile(value);
  callback(result);
};

export const checkUserCode = (rule, value, callback) => {
  if (rule.required && !value) {
    callback("身份证号不能为空");
    return;
  }
  const result = cardid(value);
  callback(result);
};

export const checkImage = (rule, value, callback) => {
  if (rule.required && (!value || !value.length)) {
    return callback(new Error("请至少上传一张图片"));
  }
  callback();
};

export const checkLongitude = (_, value, callback) => {
  if (!REGEXP_FLOAT.test(value)) {
    callback("请输入-180.0 ~ 180.0之间的正确经度");
    return;
  }
  if (-180 > Number(value) || Number(value) > 180) {
    callback("请输入-180.0 ~ 180.0之间的正确经度");
    return;
  }
  callback();
};

export const checkLatitude = (_, value, callback) => {
  if (!REGEXP_FLOAT.test(value)) {
    callback("请输入-90.0 ~ 90.0之间的正确纬度");
    return;
  }
  if (-90 > Number(value) || Number(value) > 90) {
    callback("请输入-90.0 ~ 90.0之间的正确纬度");
    return;
  }
  callback();
};

export const checkName = (rule, value, callback) => {
  if (rule.required && !value) {
    callback("姓名不能为空");
    return;
  }
  if (!REGEXP_CH.test(value)) {
    callback("请输入正确的中文名");
    return;
  }
  callback();
};

export const checkPrice = (r, v, c) => {
  if (r.required && !v && v !== 0) {
    return c(new Error("不能为空"));
  }
  if (!REGEXP_PRICE.test(v)) {
    return c(new Error("请输入正确价格"));
  }
  c();
};
